using System;
using System.IO;

using Dashboard.Util;

namespace Dashboard
{
	public class FileConnection : Connection
	{
		private string messages_directory = Path.Combine (PathFinder.UserData, "Messages");
		private string messages_extension = "xml";

		public FileConnection ()
		{

		}
		
		public override void Send (Message message)
		{
			string temp_path = Path.GetTempFileName ();
			string file_name = String.Format ("{0}.{1}", Guid.NewGuid (), messages_extension);
			string message_path = Path.Combine (messages_directory, file_name);

			using (Stream stream = File.OpenWrite (temp_path))
				Message.Serializer.Serialize (stream, message);

			File.Move (temp_path, message_path);
		}

		public override void Subscribe (string pattern, MessageHandler handler)
		{
			throw new ApplicationException ("Subscriptions not supported");
		}

		public override void Dispose ()
		{

		}
	}
}
